﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Krystalware.SlickUpload;
using Krystalware.SlickUpload.Controls;



public partial class Controls_UploadFile_UploadFile : System.Web.UI.UserControl
{
    public delegate void UploadComplete(UploadCompleteEventArgs e);
    public event UploadComplete OnUploadComplete;
    

    protected void Page_Load(object sender, EventArgs e)
    {
        if (ViewState["ShowInsertEditerButton"] != null &&
            ViewState["ShowInsertEditerButton"].ToString() == "False")
        {
            cbInsertEditer.Visible = false;
        }
        else
        {
            cbInsertEditer.Visible = true;
        }
    }

    protected void slickUpload_UploadComplete(object sender, UploadStatusEventArgs e)
    {
        if (e.UploadedFiles.Count > 0)
        {
            DataTable table = new DataTable();
            DataColumnCollection columns = table.Columns;
            columns.Add("FileID", typeof(System.String));
            columns.Add("FileName", typeof(System.String));
            columns.Add("FileLength", typeof(System.Int64));
            columns.Add("FileType", typeof(System.String));
            columns.Add("Extension", typeof(System.String));
            columns.Add("FullRemoteFileName", typeof(System.String));
            columns.Add("FullServerFileName", typeof(System.String));
            columns.Add("SourceElement", typeof(System.String));
            //columns.Add("Url", typeof(System.String));
            
            foreach (UploadedFile file in e.UploadedFiles)
            {
                FileInfo fileInfo = new FileInfo(file.LocationInfo["fileName"]);
                string fileID =Guid.NewGuid().ToString();
                string newFullFileName = fileInfo.Directory.FullName + "\\" + fileID + fileInfo.Extension;
                File.Move(file.LocationInfo["fileName"], newFullFileName);

                DataRow row = table.NewRow();
                row["FileID"] = fileID;
                row["FileName"] = file.ClientName;
                row["FileLength"] = file.ContentLength;
                row["FileType"] = file.ContentType;
                row["Extension"] = fileInfo.Extension;
                row["FullRemoteFileName"] = file.ClientPath;
                row["FullServerFileName"] = newFullFileName;
                row["SourceElement"] = file.SourceElement;
                //row["Url"] = file.ClientName;  
                table.Rows.Add(row);
            }

            if (OnUploadComplete != null)
            {
                UploadCompleteEventArgs ex = new UploadCompleteEventArgs();
                ex.UploadFile = table;
                OnUploadComplete(ex);
            }
        }
        modalPopup.Hide();
    }

    public void ModalPopupShow()
    {
        modalPopup.Show();
    }

    public void SetShowButtonClientID(string value)
    {
        ViewState["ShowButtonClientID"] = value;
    }

    public void SetSubject(string value)
    {
        literalModalPopup.Text = value;
    }

    public void SetMaxFiles(int value)
    {
        slickUpload.MaxFiles = value;
    }

    public void SetValidExtensions(string value)
    {
        //gif,png,jpg,jpeg,bmp
        slickUpload.ValidExtensions = value;
    }

    public void SetInvalidExtensionMessage(string value)
    {
        slickUpload.InvalidExtensionMessage = value;
    }

    public void SetShowInsertEditerButton(bool value)
    {
        ViewState["ShowInsertEditerButton"] = value;
    }

    public bool GetInsertEditer()
    {
        return cbInsertEditer.Checked;
    }
}


public class UploadCompleteEventArgs
{
    private DataTable uploadFile;

    public DataTable UploadFile
    {
        get { return uploadFile; }
        set { uploadFile = value; }
    }

}